package dxn.nl.export.deal.export;

import dxn.nl.file.client.model.dto.TransferMessageDto;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

@Slf4j
public class ExportSessionManager {
    private final static HashMap<String, ExportDealSteamQueryBiz> sessionMap = new HashMap<>();
    private final static HashMap<String, LocalDateTime> sessionCheckTimeMap = new HashMap<>();

    public static void finishAllExportUtil(String message)
    {
        // 使用Iterator遍历HashMap并删除特定条目
        Iterator<Map.Entry<String, ExportDealSteamQueryBiz>> iterator = sessionMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, ExportDealSteamQueryBiz> entry = iterator.next();
            entry.getValue().finish(message);
            iterator.remove();
            sessionCheckTimeMap.remove(entry.getKey());
        }
    }

    public static void addExportUtil(String sessionId,ExportDealSteamQueryBiz exportDealUtil)
    {
        sessionCheckTimeMap.put(sessionId,LocalDateTime.now());
        sessionMap.put(sessionId,exportDealUtil);
    }

    public static void checkTimeOut()
    {
        try{
            Iterator<Map.Entry<String, ExportDealSteamQueryBiz>> iterator1 = sessionMap.entrySet().iterator();
            while (iterator1.hasNext())
            {
                Map.Entry<String, ExportDealSteamQueryBiz> entry = iterator1.next();
                if(!sessionCheckTimeMap.containsKey(entry.getKey()))
                {
                    entry.getValue().finish("等待处理超时");
                    iterator1.remove();
                }
            }

            Iterator<Map.Entry<String, LocalDateTime>> iteratorCheckTime = sessionCheckTimeMap.entrySet().iterator();
            while (iteratorCheckTime.hasNext()) {
                Map.Entry<String, LocalDateTime> entry = iteratorCheckTime.next();

                if(!sessionMap.containsKey(entry.getKey()))
                {
                    iteratorCheckTime.remove();
                    continue;
                }

                if(entry.getValue() == null)
                {
                    entry.setValue(LocalDateTime.now());
                    continue;
                }

                long until = entry.getValue().until(LocalDateTime.now(), ChronoUnit.MINUTES);
                if(until > 5)
                {
                    ExportDealSteamQueryBiz baseBiz = sessionMap.get(entry.getKey());
                    baseBiz.finish("处理超时");
                    sessionMap.remove(entry.getKey());
                    iteratorCheckTime.remove();
                }
            }
        }
        catch (Exception ex)
        {
            log.error("",ex);
        }
    }


    public static String dealResponseData(TransferMessageDto messageDto)
    {
        try{
            sessionCheckTimeMap.put(messageDto.getConfig().getSessionId(),LocalDateTime.now());
            ExportDealSteamQueryBiz exportDealUtil = sessionMap.get(messageDto.getConfig().getSessionId());
            boolean continueDeal = exportDealUtil.writerData(messageDto);
            if(!continueDeal)
            {
                exportDealUtil.finish(null);
                return "处理失败";
            }
            return null;
        }
        catch (Exception ex)
        {
            log.error("",ex);
            return ex.getMessage();
        }
    }
}
